#ifndef __NET_FUN_H
#define __NET_FUN_H

#define NET_FUN

#ifdef NET_FUN
//#define NET_DEBUG  

#define NIP_NUM    2

#define NRX_LEN1  1100
#define NRX_LEN2  1100
#define NRX_LEN3  1450
#define NRX_LEN4   512
#define NRX_LEN5   512
#define NRX_LEN6   300
#define NRX_LEN7   300
#define NRX_LEN8   300

#define NTX_LEN1   1450
#define NTX_LEN2   1450 
#define NTX_LEN3   1450
#define NTX_LEN4    512
#define NTX_LEN5    512
#define NTX_LEN6    300 
#define NTX_LEN7    300 
#define NTX_LEN8    300

#define NRX_USE_LEN (NRX_LEN1+NRX_LEN2+NRX_LEN3+NRX_LEN4+NRX_LEN5+NRX_LEN6+NRX_LEN7+NRX_LEN8+20)
#define NTX_USE_LEN (NTX_LEN1+NTX_LEN2+NTX_LEN3+NTX_LEN4+NTX_LEN5+NTX_LEN6+NTX_LEN7+NTX_LEN8+20)

//////////////////////////////////////////////

#ifdef FIRST
#define EXTERN 

#if ((NTX_USE_LEN+NRX_USE_LEN)>NTR_BUF_SIZE)
NTR_error size over NTR_BUF_SIZE;
#endif

EXTERN U8 *NRX_BUFF=(U8 *)NTR_BUF_AP;
EXTERN U8 *NTX_BUFF=(U8 *)(NTR_BUF_AP+NRX_USE_LEN);

#else
#define EXTERN  extern

EXTERN U8 *NRX_BUFF;
EXTERN U8 *NTX_BUFF;
#endif


//EXTERN unsigned char NRX_BUFF[NRX_MAX_SIZE+8];
//EXTERN unsigned char NTX_BUFF[NTX_MAX_SIZE+8];

#include "net_tcpip.h"
#include "net_emac.h"

#define NET_SPATH   10
typedef struct { 
   unsigned char   nprotocol;
   unsigned short  nport;
   unsigned short  nip[2];
   short           npath;
   short           cpath;	
   unsigned short  rx_head,rx_tail,rx_max_buf;
   unsigned char   *rx_buf; 
   unsigned short  tx_head,tx_tail,tx_max_buf;
   unsigned char   *tx_buf; 
   unsigned short  udp_idt;
	
   TTCPStateMachine TCPStateMachine;         // perhaps the most important var at all ;-)
   TLastFrameSent   LastFrameSent;           // retransmission type
   unsigned short ISNGenHigh;                // upper word of our Initial Sequence Number
   unsigned long  TCPSeqNr;                   // next sequence number to send
   unsigned long  TCPUNASeqNr;                // last unaknowledged sequence number
                                             // incremented AFTER sending data
   unsigned long  TCPAckNr;                   // next seq to receive and ack to send
                                             // incremented AFTER receiving data
   unsigned char  TCPTimer;                   // inc'd each 262ms
   unsigned char  RetryCounter;               // nr. of retransmissions   
   
   unsigned char  TCPFlags;
   unsigned char  SocketStatus;
   
   unsigned short Get_SourcePort;            // segment's source port
   unsigned short Get_DestPort;              // segment's destination port
   unsigned short Send_SourcePort;           // segment's source port
   unsigned short Send_DestPort;             // segment's destination port
   
   unsigned short RemoteMAC[3];              // MAC and IP of current TCP-session
   unsigned short RemoteIP[2];
   unsigned short MyIP[2];	
   unsigned short GatewayIP[2];  
   unsigned short SubnetMask[2];
   
   unsigned short tcp_slow,tx_Window,rx_Window;
   unsigned short webnet;
} NET_PARM;

#define NET_NUM  8
EXTERN NET_PARM  NET[NET_NUM];
EXTERN unsigned short net_dly;
EXTERN unsigned char  net_rdy;
EXTERN char mac_redef;

void  net_i2c(void);
void  net_mac_set(int yy,int mn,int dd,int hh,int mm,int ss);

short net_create(short lno,unsigned char *ips,int port,unsigned char protocol);

void  net_rx_buf_write(char path,unsigned char data);
unsigned short net_rx_buf_rdy(char path);
unsigned char  net_getch(char path);
unsigned char  net_get_char(char path);
unsigned char  *net_rx_buf(char path);
void net_clr_rx_port(char path);

unsigned short net_tx_buf_rdy(char path);
void     net_tx_buf_write(char path,unsigned char data);
unsigned char net_tx_buf_read(char path);
unsigned char *net_tx_buf(char path);
void     net_clr_tx_port(char path);
void     net_puts(char path,char *data);
void     net_dputs(char path,char *data);
void     net_tx(char path,unsigned short dlen);
void     net_tx_int_ena(char path);
unsigned short  net_tx_max(char path);
unsigned short  net_rx_max(char path);
unsigned char   net_protocol(char path);
void     net_pro(void);
void     init_net(void);
#endif

#endif
